﻿#include "recv.h"


//解析json
STA unpack(int sock) {
	STA dat;
	memset(&dat,0,sizeof(dat));
	static int ret = 0;
	char buf[1024] = { 0 };
	ret = recv(sock, buf, 1024, 0);
	if(ret == -1){
		perror("recv");
		exit(-1);
	}
	printf("%s\n", buf);

	cJSON* json = NULL;
	json = cJSON_Parse(buf);
	if (json == NULL) {
		printf("接收解析error\n");
	}
	cJSON *node = NULL;

	node = cJSON_GetObjectItem(json,"type");
	if(node == NULL)
	{
		printf("can't find type\n");
		exit(-1);
	}
	if(node->type == cJSON_Number)
	{
		dat.type = node->valueint;
	}


	node = cJSON_GetObjectItem(json, "state");
	if (node == NULL) {
		printf("读取error\n");
	}
	if (node->type == cJSON_String) {
		strcpy(dat.state,node->valuestring);
	}
	return dat;
}
